//
//  NSMutableStringAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "NSMutableStringAdditions.h"

@implementation NSMutableString (Tint)



#pragma mark - URL

- (void)addQueryDictionary:(NSDictionary *)dictionary {
    NSEnumerator *keyEnumerator = [dictionary keyEnumerator];
    NSString *key = nil;
    while ((key = [keyEnumerator nextObject])) {
        NSString *value = [dictionary objectForKey:key];
        [self appendParameter:value forKey:key];
    }
}

- (void)appendParameter:(id)paramter forKey:(NSString *)key {
    if (paramter) {
        BOOL needsQuestionMark = [self rangeOfString:@"?" options:0].location == NSNotFound;
        if (needsQuestionMark) {
            [self appendString:@"?"];
        }
        
        BOOL charOnEnd = [self hasSuffix:@"&"] || [self hasSuffix:@"?"];
        if (!charOnEnd) {
            [self appendString:@"&"];
        }
        
        [self appendFormat:@"%@=%@", key, paramter];
    }
}



#pragma mark - Replacing

- (void)replaceString:(NSString *)searchString withString:(NSString *)newString {
    [self replaceString:searchString withString:newString ignoringCase:NO];
}
- (void)replaceString:(NSString *)searchString withString:(NSString *)newString ignoringCase:(BOOL)ignore {
    NSStringCompareOptions options = NSLiteralSearch;
    if (ignore)
        options = NSCaseInsensitiveSearch;
    
    [self replaceOccurrencesOfString:searchString withString:newString options:options range:NSMakeRange(0, [self length])];
}



#pragma mark - Triming

- (void)removeWhitespace {
    NSString *replaced = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    [self replaceCharactersInRange:NSMakeRange(0, [self length]) withString:replaced];
}

- (void)removeWhitespaceAndNewline {
    NSString *replaced = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //NSString *replaced = [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@""];
    [self replaceCharactersInRange:NSMakeRange(0, [self length]) withString:replaced];
}



#pragma mark - Truncating

- (void)truncateToWidth:(CGFloat)width withFont:(UIFont *)font {
    
    NSString *ellipsis = @"...";
    
    if ([self sizeWithFont:font].width > width) {
        
        width -= [ellipsis sizeWithFont:font].width;
        NSRange range = {[self length] - 1, 1};
        
        while ([self sizeWithFont:font].width > width) {
            [self deleteCharactersInRange:range];
            range.location--;
        }
        
        [self replaceCharactersInRange:range withString:ellipsis];
    }
}

@end
